package it.localhost.deepthought.one.activity;

import it.localhost.deepthought.one.R;
import it.localhost.deepthought.one.common.AdManager;
import it.localhost.deepthought.one.common.DBCheckerService;
import it.localhost.deepthought.one.fragment.MessageDialogFragment;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.android.gms.ads.AdView;
import com.google.android.gms.games.Games;

public class MenuActivity extends AbstractActivity { // NO_UCD
	private static final int REQUEST_ACHIEVEMENTS = 1;
	private static final int REQUEST_LEADERBOARD = 1;
	private static final String info = "info";
	private static final String SEP = " %";
	static final String SELECTED_LEV = "selectedLevel";
	static final String SEME = "seme";
	static final String TURNO = "turno";
	private SharedPreferences sp;
	private int selectedLevel;
	private TextView indicatorLevel;
	private Spinner spinner_turno;
	private Spinner spinner_seme;
	private SeekBar seekBarLevel;
	private LinearLayout seekBarBox;
	private ProgressBar progressBar;
	private Button next;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		// android.os.StrictMode.setThreadPolicy(new
		// StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
		// android.os.StrictMode.setVmPolicy(new
		// StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
		sp = PreferenceManager.getDefaultSharedPreferences(this);
		indicatorLevel = (TextView) findViewById(R.id.indicatorLevel);
		spinner_turno = (Spinner) findViewById(R.id.spinner_turno);
		spinner_seme = (Spinner) findViewById(R.id.spinner_seme);
		seekBarLevel = (SeekBar) findViewById(R.id.seekBarLevel);
		seekBarBox = (LinearLayout) findViewById(R.id.seekBarBox);
		next = (Button) findViewById(R.id.btn_next);
		progressBar = (ProgressBar) findViewById(R.id.levelProgressBar);
		ObjectAnimator rewardAnimm = ObjectAnimator.ofFloat(findViewById(R.id.reward), "rotationY", 30, -30);
		rewardAnimm.setRepeatCount(ObjectAnimator.INFINITE);
		rewardAnimm.setRepeatMode(ObjectAnimator.REVERSE);
		rewardAnimm.start();
		seekBarLevel.setOnSeekBarChangeListener(new OnMenuSeekBarChangeListener());
		new AdManager().load(this, ((AdView) findViewById(R.id.adView)), false);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == MainActivity.class.hashCode())
			openSnapshot();
	}

	@Override
	public void onSignIn() {
		new DBCheckerAsyncTask(MenuActivity.this).execute();
	}

	@Override
	public void onSignOut() {
		next.setVisibility(View.GONE);
		seekBarBox.setVisibility(View.GONE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_option, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.gameRule:
				new MessageDialogFragment().show(this, R.string.gameRule, R.string.gameRuleMsg, false);
				return true;
			case R.id.pieRole:
				new MessageDialogFragment().show(this, R.string.pieRole, R.string.pieRoleMsg, false);
				return true;
			case R.id.dollar:
				new MessageDialogFragment().show(this, R.string.dollar, R.string.dollarMsg, false);
				return true;
			case R.id.credits:
				new MessageDialogFragment().show(this, R.string.credits, R.string.creditsMsg, false);
				return true;
			case R.id.achievement:
				if (googleApiClient.isConnected())
					startActivityForResult(Games.Achievements.getAchievementsIntent(googleApiClient), REQUEST_ACHIEVEMENTS);
				else
					new MessageDialogFragment().show(this, R.string.error, R.string.loginReq, false);
				return true;
			case R.id.leaderboard:
				if (googleApiClient.isConnected())
					startActivityForResult(Games.Leaderboards.getLeaderboardIntent(googleApiClient, getString(R.string.leaderboard_top_rescuers_of_humanity)), REQUEST_LEADERBOARD);
				else
					new MessageDialogFragment().show(this, R.string.error, R.string.loginReq, false);
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	public void next(View v) {
		speak(R.string.presentation);
		Editor edit = sp.edit();
		edit.putInt(SEME, spinner_seme.getSelectedItemPosition());
		edit.putInt(TURNO, spinner_turno.getSelectedItemPosition());
		edit.commit();
		Intent intent = new Intent(MenuActivity.this, MainActivity.class);
		intent.putExtra(SELECTED_LEV, selectedLevel);
		startActivityForResult(intent, MainActivity.class.hashCode());
	}

	private final class OnMenuSeekBarChangeListener implements OnSeekBarChangeListener {
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			indicatorLevel.setText(progress + SEP);
			selectedLevel = progress;
		}
	}

	private class DBCheckerAsyncTask extends DBCheckerService {
		public DBCheckerAsyncTask(Activity activity) {
			super(activity);
		}

		@Override
		protected void onPostExecute(Boolean status) {
			if (status) {
				if (sp.getBoolean(info, true)) {
					new MessageDialogFragment().show(MenuActivity.this, R.string.gameRule, R.string.gameRuleMsg, false);
					sp.edit().putBoolean(info, false).commit();
				}
				next.setVisibility(View.VISIBLE);
				next.setEnabled(true);
				seekBarBox.setVisibility(View.VISIBLE);
				((TextView) findViewById(R.id.welcome)).setText(Games.getCurrentAccountName(googleApiClient));
				spinner_seme.setSelection(sp.getInt(SEME, 0));
				spinner_turno.setSelection(sp.getInt(TURNO, 0));
				selectedLevel = maxLevel;
				seekBarLevel.setMax(maxLevel);
				progressBar.setProgress(maxLevel);
				seekBarLevel.setProgress(maxLevel);
			} else
				new MessageDialogFragment().show(MenuActivity.this, R.string.error, R.string.errorMsg, true);
		}
	}
}